var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var signRouter = require('./routes/sign')

const verifyMiddleware = require('./routes/middleware/verify');

var app = express();

//解决请求跨域问题
var cors=require('cors');
app.use(cors({
    origin:['http://localhost:41644'],  //指定接收的地址
    methods:['GET','POST'],  //指定接收的请求类型
    alloweHeaders:['Content-Type','Authorization']  //指定header
}))
// view engine setup
app.set('views', path.join(__dirname, 'views'));

//安装引擎，读取html文件 
app.engine('.html', require('express-art-template'));
app.set('view engine', 'html');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));



app.use('/', indexRouter);
//先执行token解析的中间件(如果通过判断则next()到下个中间件(譬如路由)，不通过判断则直接返回错误信息)再执行接口
app.use('/sign', signRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
